const Blog = require("../models/Blog");


// 查询所有博客
exports.getBlogs = async function (page = 1, limit = 1000, where, attributes) {
    console.log("🚀 ~ file: blogServices.js:6 ~ where:", where)
    const params = where ? {
        offset: (page - 1) * limit,
        limit: +limit,
        where,
        attributes
    } : {
        offset: (page - 1) * limit,
        limit: +limit,
        attributes
    }
    const result = await Blog.findAll(params);
    return result;
}

// 增加博客
exports.addBlog = async function (blogObj) {
    const ins = await Blog.create(blogObj);
    return ins.toJSON();
}

// 博客分页查询
exports.getBlogsPage = async function (page = 1, limit = 10) {
    const result = await Blog.findAndCountAll({
        offset: (page - 1) * limit,
        limit: +limit,
    })
    return {
        total: result.count,
        datas: JSON.parse(JSON.stringify(result.rows)),
    };
}